package com.googlecode.dgwt.client.dijit.view;

import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ScrollEvent;
import com.google.gwt.event.dom.client.ScrollHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.UIObject;
import com.googlecode.dgwt.client.dijit.base.HasScrollBars;
import com.googlecode.dgwt.client.dijit.base.manager.ScrollManager;
import com.googlecode.dgwt.client.dijit.view.render.ScrollRender;
import com.googlecode.dgwt.client.dijit.view.render.base.CellRender;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class GridView extends TableView implements HasScrollBars {

    @Override
    protected CellRender createRender(){
        return new ScrollRender(super.createRender());
    }

    protected void setScrollableElement(Element scrollableElement){
        setAttachedElement("scrollableElement", scrollableElement);
    }

    @Override
    public Element getScrollableElement(){
        return getAttachedElement("scrollableElement");
    }

    public HandlerRegistration addScrollHandler(ScrollHandler handler) {
        return addDomHandler(handler, ScrollEvent.getType());
    }
    
    protected ScrollManager getScrollManager(){
        return ScrollManager.get();
    }

    public void ensureVisibility(UIObject item) {
        getScrollManager().ensureVisibility(this, item);
    }
    
    public int getHorizontalScrollPosition() {
        return getScrollManager().getHorizontalScrollPosition(this);
    }
    
    public int getVerticalScrollPosition() {
        return getScrollManager().getVerticalScrollPosition(this);
    }
    
    public void scrollToBottom() {
        getScrollManager().scrollToBottom(this);
    }
    
    public void scrollToLeft() {
        getScrollManager().scrollToLeft(this);
    }
    
    public void scrollToRight() {
        getScrollManager().scrollToRight(this);
    }
    
    public void scrollToTop() {
        getScrollManager().scrollToTop(this);
    }
    
    public void setHorizontalScrollPosition(int position) {
        getScrollManager().setHorizontalScrollPosition(this, position);
    }

    public void setVerticalScrollPosition(int position) {
        getScrollManager().setVerticalScrollPosition(this, position);
    }

    @Override
    public ScrollBarVisibility getHorizontalScrollBar() {
        return getScrollManager().getHorizontalScrollBar(this);
    }

    @Override
    public ScrollBarVisibility getVerticalScrollBar() {
        return getScrollManager().getVerticalScrollBar(this);
    }

    @Override
    public void setHorizontalScrollBar(ScrollBarVisibility visibility) {
        getScrollManager().setHorizontalScrollBar(this, visibility);
    }

    @Override
    public void setScrollBars(ScrollBarVisibility vertical, ScrollBarVisibility horizontal) {
        getScrollManager().setScrollBars(this, vertical, horizontal);
    }

    @Override
    public void setVerticalScrollBar(ScrollBarVisibility visibility) {
        getScrollManager().setVerticalScrollBar(this, visibility);
    }

}
